package phase01module02.homework;

import java.util.Scanner;

/**
 * 绘制棋盘 - 写一个成员方法实现
 *
 * 提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现
 *
 * 每当一方下棋后判断是否获胜 - 写一个成员方法实现
 */
public class Chess {

    //1.创建二位数组，封装Chess类（三步走）
    //绘制棋盘
    private String[][] chessBoard;
    //定义黑白棋
    private char white; //白子
    private char black; //黑子
    //棋盘大小
    private int rows;   //行
    private int cols;   //列

    //无参构造方法
    public Chess() {
    }

    /**
     * 公有的set 有参构造方法
     * @param white
     * @param black
     * @param rows
     * @param cols
     */
    public Chess(char white, char black, int rows, int cols) {
        setChessBoard(rows, cols);
        setWhite(white);
        setBlack(black);
        setRows(rows);
        setCols(cols);
    }

    /**
     * 公有get,set方法
     * @return
     */
    public void setChessBoard(int rows, int cols) {
        chessBoard = new String[rows][cols];
    }

    public char getWhite() {
        return white;
    }

    public void setWhite(char white) {
        this.white = white;
    }

    public char getBlack() {
        return black;
    }

    public void setBlack(char black) {
        this.black = black;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getCols() {
        return cols;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }

    //2.绘制棋盘
    public void getChessBoard() {
        for(int i = 0; i < chessBoard.length; i++){
            for(int j = 0; j < chessBoard[i].length; j++){
                //打印第一行，转为十六进制
                if(0 == i){
                    chessBoard[i][j] = Integer.toHexString(j-1);
                }
                else if(0 == j){
                    chessBoard[i][j] = Integer.toHexString(i-1);
                }else{
                    chessBoard[i][j] = "+";
                }
            }
        }
        chessBoard[0][0] = " ";
    }

    //3. 打印棋子
    public void showChessMan() {
        for (String[] strings : chessBoard) {
            for (int j = 0; j < chessBoard[0].length; j++) {
                if ("白".equals(strings[j])) {
                    System.out.print(white + " ");
                } else if ("黑".equals(strings[j])) {
                    System.out.print(black + " ");
                } else {
                    System.out.print(strings[j] + " ");
                }
            }
            System.out.println();
        }
    }

    //接收游戏入口传来的参数，判断是否获胜
    private Boolean isWin(int x, int y, String chess, int rows, int cols){
        int countRow = -1;
        int countCol = -1;
        int countLeftDiagonal = -1;
        int countRightDiagonal = -1;
        int a;
        int b;

        //无限循环，判断获胜条件
        a = x;
        b = y;
        while(true){
            //行：从右到左判断5个棋子是否连在一起，如果其中有一个棋子断开就执行break，b=0时执行break
            if(chess.equals(chessBoard[a][b])){
                countRow++;
                if(0 == b){
                    break;
                }
                b--;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //行：左到右判断5个棋子是否连在一起，如果其中一个棋子断开，执行break，b到数组末尾也执行break
            if(chess.equals(chessBoard[a][b])){
                countRow++;
                if((cols - 1) == b){
                    break;
                }
                b++;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while (true){
            //列：是否5个棋子连在一起 下到上
            if(chess.equals(chessBoard[a][b])){
                countCol++;
                if(0 == a){
                    break;
                }
                a--;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //列：是否5个棋子连在一起 上到下
            if(chess.equals(chessBoard[a][b])){
                countCol++;
                if((rows - 1) == a){
                    break;
                }
                a++;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //左到右斜角线 5个棋子是否相连
            if(chess.equals(chessBoard[a][b])){
                countLeftDiagonal++;
                if(0 == a || 0 == b){
                    break;
                }
                a--;
                b--;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //右到左斜角线 5个棋子是否相连
            if(chess.equals(chessBoard[a][b])){
                countLeftDiagonal++;
                if((rows - 1) == a || (cols - 1) == b){
                    break;
                }
                a++;
                b++;
            }else{
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //判断右上到左下5个棋子是否相连
            if(chess.equals(chessBoard[a][b])){
                countRightDiagonal++;
                if(0 == a || (cols - 1) == b){
                    break;
                }
                a--;
                b++;
            }else {
                break;
            }
        }

        a = x;
        b = y;
        while(true){
            //判断左下到右上5个棋子是否相连
            if(chess.equals(chessBoard[a][b])){
                countRightDiagonal++;
                if((rows - 1) == a || 0 == b){
                    break;
                }
                a++;
                b--;
            }else{
                break;
            }
        }

        System.out.println("countRow:" + countRow);
        System.out.println("countCol:" + countCol);
        System.out.println("countLeftDiagonal:" + countLeftDiagonal);
        System.out.println("countRightDiagonal:" + countRightDiagonal);

        return (countRow >= 5 || countCol >= 5 || countLeftDiagonal >= 5 || countRightDiagonal >= 5);
    }


    //4. 编写游戏入口
    public void startGame() {
        Scanner scanner = new Scanner(System.in);

        boolean flag = true;
        int x;
        int y;
        String chess;

        while(true){
            if(flag){
                System.out.println("白子落");
                chess = "白";
            }else{
                System.out.println("黑子落");
                chess = "黑";
            }

            x = scanner.nextInt() + 1;
            y = scanner.nextInt() + 1;

            if(x < 0 || y < 0 || x > 16 || y > 16){
                System.out.println("超出棋盘范围，请重新落子");
                continue;
            }
            if("+".equals(chessBoard[x][y])){
                chessBoard[x][y] = chess;
            }else{
                System.out.println("当前坐标不可落子，请重新落子");
                continue;
            }
            showChessMan();
            flag = !flag;

            boolean isWin = isWin(x, y, chess, getRows(), getCols());
            if(isWin) {
                System.out.println("恭喜" + chess + "方获胜");
                break;
            }
        }
        //游戏获胜后，释放内存空间
        scanner.close();
    }
}